package com.atm.tone;

import java.io.File;

import android.app.Activity;
import android.content.ContentValues;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.RingtonePreference;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ToneActivity extends Activity {
    
    ListView list;
    String rootPath;
    String selectedFileName;
    String[] fileList;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String state = android.os.Environment.getExternalStorageState();
        System.out.println(" State : "+state);
        if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
            //throw new IOException("SD Card is not mounted.  It is " + state + ".");
            return;
        }
        rootPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        
        System.out.println("Data Directory "+Environment.getDataDirectory().getAbsolutePath());
        System.out.println("Root Directory "+Environment.getRootDirectory().getAbsolutePath());
        System.out.println("External Storage Directory "+Environment.getExternalStorageDirectory().getAbsolutePath());
        File rootDir = new File(rootPath);
        fileList = rootDir.list();
        
        list = (ListView)findViewById(R.id.file_list);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
        list.setAdapter(adapter);
        registerForContextMenu(list);
        /*list.setOnLongClickListener(new OnLongClickListener() {
            
            @Override
            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                System.out.println(" Pressed at "+list.getSelectedItem());
                return false;
            }
        });*/
        
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0,0,0,"Set as Ringtone");
        menu.add(0,1,1,"Set as Alarm Tone");
        menu.add(0,2,2,"Set as Message Tone");
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        super.onContextItemSelected(item);
        System.out.println(" Item Selected "+item.getItemId()+" list item id "+list.getSelectedItemId()+" posn "+list.getSelectedItemPosition());
        String name = fileList[(int) list.getSelectedItemId()];
        System.out.println(" name Selected "+name);
        File newFile = new File(rootPath+"/"+name);
        System.out.println("New "+newFile.getAbsolutePath());
        setRingTone(newFile);
        return false;
    }
    
    void setRingTone(File file){
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, file.getName());
        //values.put(MediaStore.MediaColumns.SIZE, 1024);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");    // assuming it's an mpeg, of course
        //values.put(MediaStore.Audio.Media.ARTIST, "<<yourArtistNameHere>>");
        // values.put(MediaStore.Audio.Media.DURATION, duration);  // doesn't appear to be necessary if you don't know
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());  
        Uri newUri = getContentResolver().insert(uri, values);

        RingtoneManager.setActualDefaultRingtoneUri(this,
                                        RingtoneManager.TYPE_RINGTONE,
                                        newUri);
    }
}